package org.myrpc.rpc;

import org.myrpc.server.RpcServer;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class RpcServerFactory implements ApplicationContextAware{
	/*实现该接口就可以在xml中使用class标签注册该类*/
	
	//这两个属性需要通过xml来设置
	private int port;
	private String address;
	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public static ApplicationContext applicationContext;
	
	//加载配置文件对象
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		RpcServerFactory.applicationContext = applicationContext;
	}
	
	public static <T> T getBeanImpl(Class<T> clazz) {
		return applicationContext.getBean(clazz);
	}
	
	//初始化服务
	public void init() {
		RpcServer server = new RpcServer(port,address);
		server.start();//通过线程开启，避免init方法无法退出（因为Rpcserver中有socket阻塞）
	}
	
}
